package com.example.demo.service;

import com.example.demo.mapper.AdminMapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.dbRole;
import com.example.demo.pojo.dbUser;
import com.example.demo.vo.Result;
import com.example.demo.vo.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.util.List;
import java.util.Optional;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<UserDetail> selectByUserAll(Integer ID) {
        List<UserDetail> list=adminMapper.selectByUserAll(ID);
//        System.out.println(list);
        return list;
    }

    @Override
    public int deleteUserById(Integer id) {
        int i=0;
        try {
            i=userMapper.deleteById(id);
                if (i==1)
                    i=roleMapper.deleteById(id);
            return i;
            } catch (Exception e) {
                e.printStackTrace();
                return i;
            }
    }

    @Override
    public Result add(UserDetail userDetail) {
        dbRole role=new dbRole();
        dbUser user=new dbUser();
        role.setUser_id(userDetail.getID())
                .setEdit_below(userDetail.getEdit_below())
                .setEdit_middle(userDetail.getEdit_middle())
                .setEdit_top(userDetail.getEdit_top());
        user.setID(userDetail.getID())
                .setDAge(userDetail.getDAge())
                .setDName(userDetail.getDName())
                .setDSex(userDetail.getDSex())
                .setDPwd(userDetail.getDPwd())
                .setDAdmin(userDetail.isDAdmin());
        try {
            userMapper.insert(user);
            role.setUser_id(user.getID());
            roleMapper.insert(role);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("出错");
        }
    }

    @Override
    public Result update(UserDetail userDetail) {
        dbRole role=new dbRole();
        dbUser user=new dbUser();
        role.setUser_id(userDetail.getID())
                .setEdit_below(userDetail.getEdit_below())
                .setEdit_middle(userDetail.getEdit_middle())
                .setEdit_top(userDetail.getEdit_top());
        user.setID(userDetail.getID())
                .setDAge(userDetail.getDAge())
                .setDName(userDetail.getDName())
                .setDSex(userDetail.getDSex())
                .setDPwd(userDetail.getDPwd())
                .setDAdmin(userDetail.isDAdmin());
        System.out.println(user);
        System.out.println(role);
        try {
            userMapper.update(user);
            role.setUser_id(user.getID());
            roleMapper.update(role);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("出错");
        }
    }
}
